package org.yxing.palpalgps.activity;

import java.util.List;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.yxing.palpalgps.AbstractActivity;
import org.yxing.palpalgps.R;
import org.yxing.palpalgps.adapter.FriendAlertAdapter;
import org.yxing.palpalgps.http.AsyncRequest;
import org.yxing.palpalgps.http.AsyncRequest.FetchListener;
import org.yxing.palpalgps.models.ListUserItem;
import org.yxing.palpalgps.models.User;
import org.yxing.palpalgps.oauth.OAuth;
import org.yxing.palpalgps.utils.Helper;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class FriendAlertActivity extends AbstractActivity implements OnItemClickListener{
    
    private static final String FRIEND_LIST = "friend/list";
    private static final String FRIEND_ADD = "friend/add";
    private static final String FRIEND_REJECT = "friend/reject";
    
    private ListView mListView;
    private FriendAlertAdapter adapter;
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.friend_alerts);
        setTitle(getString(R.string.friend_alert));
        
        mListView = (ListView) findViewById(android.R.id.list);
        adapter = new FriendAlertAdapter(this);
        mListView.setAdapter(adapter);
        mListView.setOnItemClickListener(this);
        
        fetchData();
    }
    
    private void fetchData() {
        AsyncRequest ar = new AsyncRequest(this);
        ar.fetch(FRIEND_LIST, new FetchListener() {
            
            public void onError(int code, String msg) {
                
            }
            
            public void onComplete(JSONObject response) {
                try {
                    JSONObject result = response.getJSONObject(Helper.RESULT);
                    JSONArray approves = result.getJSONArray("approves");
                    for (int i = 0; i < approves.length(); i ++) {
                        JSONObject json = approves.getJSONObject(i);
                        ListUserItem model = new ListUserItem();
                        model.setUid(json.getInt(User.KEY_UID));
                        model.setName(json.getString(User.KEY_NAME));
                        model.setPortrait(json.getString(User.KEY_FACE));
                        model.setGender(json.getString(User.KEY_GENDER));
                        
                        adapter.add(model);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void onItemClick(AdapterView<?> parent, View view, final int position, final long id) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        
        builder.setMessage(getString(R.string.request_friend_message));
        builder.setPositiveButton(R.string.accept, new OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                request(FRIEND_ADD, position, id);
            }
        });
        builder.setNegativeButton(R.string.reject, new OnClickListener() {
            
            public void onClick(DialogInterface dialog, int which) {
                request(FRIEND_REJECT, position, id);
            }
        });
        builder.create();
        builder.show();
    }
    
    public void request(final String url, final int position, long fuid) {
        AsyncRequest ar = new AsyncRequest(this);
        List<BasicNameValuePair> params = OAuth.createAuthParams();
        params.add(new BasicNameValuePair("fuid", Long.toString(fuid)));
        ar.fetch(url, params, new FetchListener() {
            
            public void onError(int code, String msg) {
                
            }
            
            public void onComplete(JSONObject response) {
                adapter.remove(position);
            }
        });
    }
}
